package com.jinlejia.feedback.idea.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.feedback.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author SongJiaTai
 * @description:问题类型筛选第一列Adapter
 * @date :2019/6/25 18:55
 */
public class FBFilter1Adapter extends YfListAdapter<Map<String,Object>>{

    private int cur_position = -1;
    private Map<Integer,Boolean> checkMap = new HashMap<>();
    private Context context;

    public FBFilter1Adapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    public FBFilter1Adapter(Context context, ArrayList<Map<String, Object>> data) {
        super(data);
        this.context = context;
    }

    @Override
    public int getItemCount() {
        for (int i = 0; i < mData.size(); i++) {
            if (cur_position == i){
                checkMap.put(i,true);
            }else {
                checkMap.put(i,false);
            }
        }
        return super.getItemCount();
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_item_filter_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_empty_layout, parent, false);

        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {
        ((ViewHolder) viewHolder).checkbox.setText(mData.get(i).get("problemTypeName").toString());
        if (checkMap.get(i)){
            ((ViewHolder) viewHolder).checkbox.setChecked(true);
            ((ViewHolder) viewHolder).checkbox.setBackgroundResource(R.color.white_press);
        }else {
            ((ViewHolder) viewHolder).checkbox.setChecked(false);
            ((ViewHolder) viewHolder).checkbox.setBackgroundResource(R.color.btn_register_normal);
        }
        viewHolder.itemView.setTag(mData.get(i));
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cur_position != -1){
                    checkMap.put(cur_position,false);
                    notifyItemChanged(cur_position);
                    onItemRemoveListener.onItemRemove(mData.get(cur_position));
                }
                if( cur_position != i ){
                    cur_position = i;
                    checkMap.put(i,true);
                    notifyItemChanged(i);
                    onItemSelectedListener.onItemSelected(mData.get(i));
                }else{
                    if( cur_position != -1 )
                        onItemRemoveListener.onItemRemove(mData.get(cur_position));
                    checkMap.put(cur_position,false);
                    notifyItemChanged(cur_position);
                    cur_position = -1;
                }
            }
        });
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_footer_loading, parent, false);
        return new YfSimpleViewHolder(view);
    }

    static final class ViewHolder extends RecyclerView.ViewHolder {
        CheckBox checkbox;
        public ViewHolder(View itemView) {
            super(itemView);
            checkbox = (CheckBox) itemView.findViewById(R.id.checkbox);
        }
    }

    public interface OnItemSelectedListener{
        void onItemSelected(Map<String, Object> map);
    }
    private OnItemSelectedListener onItemSelectedListener;
    public void setOnItemSelectedListener( OnItemSelectedListener listener ){
        this.onItemSelectedListener = listener;
    }


    /**
     * 移除
     */
    public interface OnItemRemoveListener {
        void onItemRemove(Map<String, Object> map);
    }

    private OnItemRemoveListener onItemRemoveListener;

    public void setOnItemRemoveListener(OnItemRemoveListener listener) {
        this.onItemRemoveListener = listener;
    }
}
